﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class RoomNumberDAL
    {
        public RoomNumberDAL(HoteContent hoteContent)
        {
            HoteContent = hoteContent;
        }

        public HoteContent HoteContent { get; }
        /// <summary>
        /// 房号添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int RoomNumberAdd(RoomNumberModel info)
        {
            try
            {
                var res = HoteContent.roomNumberModels.Count(o => o.NumberName == info.NumberName);
                if (res > 0)
                {
                    return -1;//用户名已存在
                }
                HoteContent.roomNumberModels.Add(info);
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount">页容量</param>
        /// <param name="totalPage">页码</param>
        /// <param name="Pageindex">当前页</param>
        /// <param name="PageSize">页码</param>
        /// <param name="NumberName">房号</param>
        /// <param name="createtime">创建时间</param>
        /// <returns></returns>
        public IQueryable<RoomNumber> GetRoomNumberList(out int totalCount, out int totalPage, int Pageindex = 1, int PageSize = 2,string? NumberName="",DateTime? createtime=null)
        {
            try
            {
                var list = HoteContent.roomNumberModels.Join(HoteContent.roomTypeModels, a => a.RoomTypeId, b => b.Id, (a, b) => new RoomNumber
                {
                    NumberName=a.NumberName,
                    Updatetime=a.Updatetime,
                    Desc=a.Desc,
                    IsDelete=a.IsDelete,
                    Createtime=a.Createtime,
                    State=a.State,
                    Order=a.Order,
                    RoomTypeId=a.RoomTypeId,
                    Id=a.Id,
                    RoomTypeName=b.RoomTypeName
                }).Where(o => o.IsDelete == false);
                if (!string.IsNullOrEmpty(NumberName))
                {
                    list = list.Where(o => o.NumberName.Contains(NumberName));
                }
                if (createtime!=null)
                {
                    list = list.Where(o => o.Createtime> createtime);
                }
                totalCount = list.Count();
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / PageSize);
                list = list.OrderByDescending(o => o.Id).Skip((Pageindex - 1) * PageSize).Take(PageSize);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int RoomNumberDel(int id)
        {
            try
            {
                var res = HoteContent.roomNumberModels.Find(id);
                if (res != null)
                {
                    res.IsDelete = true;
                }
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int RoomNumberDelAll(string ids)
        {
            try
            {
                var res = ids.Split(',');
                foreach (var id in res)
                {
                    var info=HoteContent.roomNumberModels.Find(Convert.ToInt32(id));
                    info.IsDelete= true;
                }
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int RoomNumberUptAll(string ids,int state)
        {
            try
            {
                var res = ids.Split(',');
                foreach (var id in res)
                {
                    var info = HoteContent.roomNumberModels.Find(Convert.ToInt32(id));
                    info.State= state;
                }
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Id查询(反填)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RoomNumberModel GetRoomNumberId(int id)
        {
            try
            {
                return  HoteContent.roomNumberModels.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public  int GetRoomNumberNumUpdate(RoomNumberModel info)
        {
            try
            {
                var res = HoteContent.roomNumberModels.Count(o => o.IsDelete == false && o.NumberName == info.NumberName && o.Id != info.Id);
                if (res > 0)
                {
                    return -1;
                }
                HoteContent.roomNumberModels.Update(info);
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
